package com.gxa.autoparts.portal.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.portal.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author R
 * @data 2022-01-10 18:07
 */
@RestController
@RequestMapping("/order")
@Api("下订单接口")
public class OrderController {

    @Autowired
    OrderService orderService;



    @PostMapping("/add")
    @ApiOperation("创建订单接口")
    public Map add(@RequestBody Map map){
        Long userId = Long.parseLong(map.get("userId").toString());
        Long addressId = Long.parseLong(map.get("addressId").toString());
        Object skuNumber = map.get("skuWithNumber");
        List skuWithNumber = (List)skuNumber;
        System.out.println(skuWithNumber);
        Long couponId = 0L;
        if (map.get("couponId") != null){
            couponId = Long.parseLong(map.get("couponId").toString());
        }
        Double freight = Double.parseDouble(map.get("freight").toString());
        String remarks = map.get("remarks").toString();
        Long orderId = orderService.addOrder(userId, addressId, skuWithNumber, remarks, couponId, freight);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(orderId);
        return jsonResult.getDataMap();
    }


}
